const express = require('express');
const router = express.Router();
const comments = require('../../service/comments');
router.post('/getIdContent', async (req, res) => {
    let id = +req.body.id;
    let ins = await comments.selectComment(id);
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })

    } else {
        res.status(500).send({
            msg: 'error',
            data: {
                ins,
                falg: false
            }
        })
    }
})
// 添加评论
router.post('/addComment', async (req, res) => {
    const ins = await comments.addComment(req.body.obj);
    if (ins.id) {
        res.send({
            msg: '成功！',
            data: {
                ins,
                falg: true
            }
        })
    } else {
        res.status(500).send({
            msg: ins.errors
        })
    }

})

//更新观看量
router.post('/lookup', async (req, res) => {
    await comments.updateLook(req.body.id);
    res.send({
        msg: '成功！',
    })
})

//查看所有评论
router.post('/get_all_comment', async (req, res) => {
    const ins = await comments.get_all_comment();
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })

    } else {
        res.status(500).send({
            msg: 'error',
            data: {
                ins,
                falg: false
            }
        })
    }
})

//删除评论
router.post('/del_comment', async (req, res) => {
    try {
        if (req.body.id) {
            const ins = await comments.del_comment(+req.body.id);
            if (+req.body.aId != -1) {
                await comments.updateLook(+req.body.aId, false);
            }
            res.send({
                msg: '成功！',
                ins
            })
        }
    } catch (err) {
        res.status(500).send({
            msg: 'error',
            data: {
                falg: false
            }
        })
    }
})

//回复
router.post('/rep_comment', async (req, res) => {
    const ins = await comments.hf_comment(+req.body.id, req.body.email, req.body.txt, req.body.title)
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })

    } else {
        res.status(500).send({
            msg: 'error',
            data: {
                ins,
                falg: false
            }
        })
    }
})


module.exports = router;